import type { RequestOption } from "../../index.js";
import type { RequestFieldOption } from "../../req/RequestOption.js";
import { fields, Operation } from "./util.js";

const addr: RequestFieldOption = {
    name: "起始地址",
    kind: "u16",
    value: 0,
};

const writeSystem: RequestOption = {
    name: "0x03 写变量 - 系统",
    description: "设置系统变量的值",
    fields: [
        fields.op(Operation.WriteVariable),
        fields.crc,
        fields.length,
        {
            name: "设备id",
            kind: "u16",
            value: 0,
            readonly: true,
        },
        {
            name: "寄存器类型",
            kind: "enum",
            value: 0x0,
            enums: {
                LB: 0x0,
                LW: 0x1,
                LHB: 0x2,
                LHW: 0x3,
                LSB: 0x4,
                LSW: 0x5,
                IDX: 0x6,
            },
        },
        addr,
        {
            name: "value",
            kind: "list",
            value: [],
        },
    ],
};

const writeModbus: RequestOption = {
    name: "0x03 写变量 - modbus",
    description: "设置 modbus 设备的变量",
    fields: [
        fields.op(Operation.WriteVariable),
        fields.crc,
        fields.length,
        {
            name: "设备id",
            kind: "u16",
            value: 1,
        },
        {
            name: "寄存器类型",
            kind: "enum",
            value: 0x00,
            enums: {
                线圈寄存器: 0x0,
                离散输入寄存器: 0x1,
                保持寄存器: 0x2,
                输入寄存器: 0x3,
            },
        },
        addr,
        {
            name: "value",
            kind: "list",
            value: [],
        },
    ],
};

export const write = {
    writeSystem,
    writeModbus,
};
